package com.joysuch.wwyt.api.dataaccess;

import com.joysuch.wwyt.api.dataaccess.bean.OrientationSearchBean;
import com.joysuch.wwyt.api.dataaccess.bean.OrientationV3AlarmReqBean;
import com.joysuch.wwyt.api.service.AlarmReSubscribeService;
import com.joysuch.wwyt.bp.entity.contractor.BpContractor;
import com.joysuch.wwyt.bp.repository.BpContractorDao;
import com.joysuch.wwyt.config.RedisClient;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.monitoringcenter.service.LocateServerService;
import com.joysuch.wwyt.repeatsubmit.NoRepeatSubmit;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.repository.RiskDistrictDao;
import com.joysuch.wwyt.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "/data-access 定位数据同步")
@RequestMapping("/bp/data-access/")
public class OrientationDataSyncController {

    @Resource
    private RedisClient redisClient;
    @Autowired
    private OrientationDataSyncService service;
    @Autowired
    private RiskDistrictDao riskDistrictDao;
    @Autowired
    private BpContractorDao bpContractorDao;
    @Autowired
    private LocateServerService locateServerService;

    @Autowired
    private AlarmReSubscribeService alarmReSubscribeService;

    @GetMapping("riskDistrict")
    @ApiOperation("风险区域同步至人员定位")
    public ResultBean riskDistrict() {
        List<RiskDistrict> all = riskDistrictDao.findAll();
        service.riskDistrict(all, "addArea");
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("alarmTemplate")
    @ApiOperation("获取报警模板")
    @RequiresPermissions("bp:data-access:alarmTemplate")
    public ResultBean alarmTemplate(@RequestBody OrientationSearchBean bean) {
        return service.alarmTemplate(bean);
    }

    @PostMapping("pageCertificateOrAreaAlarm")
    @ApiOperation("获取作业/区域报警")
    public ResultBean pageCertificateOrAreaAlarm(@RequestBody OrientationSearchBean bean) {
        return service.getPageCertificateOrAreaAlarm(Context.getCurrentTenantId(), Context.getCompanyCode().substring(0, 7), bean);
    }

    @PostMapping("pageSolveAlarm")
    @ApiOperation("批量处理作业/区域报警")
    public ResultBean pageSolveAlarm(@RequestBody OrientationSearchBean bean) {
        return service.pageSolveAlarm(bean);
    }

    @PostMapping("contractor")
    @ApiOperation("承包商同步至人员定位")
    public ResultBean contractor() {
        List<BpContractor> all = bpContractorDao.findAll();
        service.contractor(all, "saveContractor");
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("checkSnInArea")
    @ApiOperation("sn号是否在区域")
    public ResultBean checkSnInArea(Long userId, Long pointId) {
        return service.checkSnInArea(userId, pointId);
    }

    @GetMapping("getAlarmNumber")
    @ApiOperation("获取报警总数")
    public ResultBean getAlarmNumber() {
        Integer alarmNumber = service.getAlarmNumber(Context.getCurrentTenantId(), Context.getCompanyCode().substring(0, 7));
        return ResultBean.success(alarmNumber);
    }

    @GetMapping("getAlarmTypesNumber")
    @ApiOperation("获取各类型报警数")
    public ResultBean getAlarmTypesNumber() {
        Map<String, Integer> map = service.getAlarmTypesNumber(Context.getCurrentTenantId(), Context.getCompanyCode().substring(0, 7));
        return ResultBean.success(map);
    }

    @GetMapping("getV2Token")
    @ApiOperation("获取token")
    public ResultBean getV2Token() {
        return ResultBean.success(service.getV2Token());
    }

    @GetMapping("getV3ScreenToken")
    @ApiOperation("获取大屏token")
    public ResultBean getV3ScreenToken() {
        return ResultBean.success(service.getV3ScreenToken());
    }

    @GetMapping("getV3BindersVersion")
    @ApiOperation("获取人员版本号")
    public ResultBean getV3BindersVersion() {
        return ResultBean.success(service.getV3BindersVersion(new OrientationSearchBean(locateServerService.getBuildId()), Context.getCurrentTenantId(), Context.getCompanyCode()));
    }

    @GetMapping("getV3ScreenBinders")
    @ApiOperation("获取大屏人员列表")
    public ResultBean getV3ScreenBinders() {
        return ResultBean.success(service.getV3ScreenBinders(new OrientationSearchBean(locateServerService.getBuildId()), Context.getCurrentTenantId(), Context.getCompanyCode()));
    }

    @GetMapping("getV3ScreenAreaPerson")
    @ApiOperation("获取大屏人员的区域列表")
    public ResultBean getV3ScreenAreaPerson() {
        return ResultBean.success(service.getV3ScreenAreaPerson(Context.getCurrentTenantId(), Context.getCompanyCode()));
    }

    @GetMapping("getV3ScreenRegionalPopulation")
    @ApiOperation("获取大屏人车列表")
    public ResultBean getV3ScreenRegionalPopulation(Long areaId) {
        return ResultBean.success(service.getV3ScreenRegionalPopulation(areaId));
    }

    @GetMapping("batchApiV3Subscribe")
    @ApiOperation("5.3人员报警订阅")
    public ResultBean batchApiV3Subscribe() {
        service.batchApiV3Subscribe();
        return ResultBean.success(Status.SUCCESS);
    }

    @GetMapping("getV3Subscribe")
    @ApiOperation("5.3人员报警订阅")
    public ResultBean getV3Subscribe(String type, String url) {
        if (service.getV3Subscribe(type, url)) {
            return ResultBean.success(Status.SUCCESS);
        } else {
            return ResultBean.fail(Status.SUBSCRIPTION_FAILED);
        }
    }

    @PostMapping("getV3AreaAlarm")
    @ApiOperation("5.3区域报警")
    public ResultBean getV3AreaAlarm(@RequestBody OrientationV3AlarmReqBean reqBean) {
        service.getV3Alarm(reqBean);
        return ResultBean.success("");
    }

    @PostMapping("getV3jobAlarmEvent")
    @ApiOperation("5.3作业报警")
    public ResultBean getV3jobAlarmEvent(@RequestBody OrientationV3AlarmReqBean reqBean) {
        service.getV3Alarm(reqBean);
        return ResultBean.success("");
    }

    @PostMapping("getV3CarAlarm")
    @ApiOperation("5.3车辆报警")
    public ResultBean getV3CarAlarm(@RequestBody OrientationV3AlarmReqBean reqBean) {
        service.getV3Alarm(reqBean);
        return ResultBean.success("");
    }

    @GetMapping("ipTest")
    @ApiOperation("ipTest")
    public ResultBean ipTest(Integer type) {
        String ip = service.ipTest(type);
        return ResultBean.success(ip);
    }

    @GetMapping("getScUserData")
    @ApiOperation("sc获取人员信息")
    public ResultBean getScUserData() {
        String orgCode = Context.getCurrentOrgCode();
        Long tenantId = Context.getCurrentTenantId();
        return ResultBean.success(service.getScUserData(orgCode, tenantId));

    }

    @NoRepeatSubmit(seconds = 60)
    @GetMapping("reSubscribeAlarm")
    public ResultBean reSubscribeAlarm(String orgCode, String tenentId) {
        if (StringUtil.isEmpty(orgCode) || StringUtil.isEmpty(tenentId)) {
            return ResultBean.wrapErrorResultDefault("orgCode 和 tenentId 不可为空！");
        }
        alarmReSubscribeService.reSubscribeAlarm(orgCode, Long.valueOf(tenentId));
        return ResultBean.success(Status.SUCCESS);
    }

}
